//
//  MainTabBar.swift
//  SinaWeibo
//
//  Created by yhlun on 15/11/8.
//  Copyright © 2015年 yhlun. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

 
    //默认的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpUI()
    }
    
    private func setUpUI() {
    
        addSubview(composeBtn)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //手动修改所有按钮位置
        //遍历所有视图
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //定义递增变量
        var index: CGFloat = 0
        for subview in subviews {
        
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                //修改frame
                subview.frame = CGRectOffset(rect, index * w, 0)
                index += index == 1 ? 2 : 1
                
            }
        }
        
        //设计撰写按钮的位置
        composeBtn.frame = CGRectOffset(rect, w * 2, 0)
        bringSubviewToFront(composeBtn)
    }
    
    //MARK:  懒加载 撰写按钮
    lazy var composeBtn : UIButton = {
    
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        return btn
    }()
    
}
